#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import os
import fileutils

SUFFIX_PVR = '.pvr'

source = None
target = None

def pvrtopng(file): 
    print (file)
    target_dir = file.replace(source, target)
    new_file = target_dir + SUFFIX_PVR
    fileutils.copyfile(file, new_file)
    
    dir_name = os.path.dirname(new_file)
    file_name = os.path.basename(new_file)

    png = os.path.join(dir_name, file_name.replace('.pvr', ''))
    plist = os.path.join(dir_name, file_name.replace('.pvr', '.plist'))
    cmd = 'TexturePacker.exe "%s" --sheet "%s" --data "%s" --opt RGBA8888 --allow-free-size --algorithm Basic --no-trim --dither-fs' % (new_file, png, plist)
    os.system(cmd)
    if os.path.isfile(plist):
        fileutils.rmfile(plist)
    if os.path.isfile(file):
        fileutils.rmfile(file)
        fileutils.copyfile(png, file)
    pass


def walkfile(source):
    def __walk__(file):
        pvrtopng(file)
        pass
    fileutils.walkfile(source, __walk__, ['.pvr', '.jpg', '.png'])
    pass

if __name__ == '__main__':
    source = 'D:\\tmp\\test9\\a\\assets\\res'
    target = 'D:\\tmp\\test9\\a\\assets\\res_out'
    walkfile(source)
    print ('aaa')